package com.example.lotterysystem.service.iml;
import com.example.lotterysystem.controller.param.CreatePrizeParam;
import com.example.lotterysystem.controller.param.PageListParam;
import com.example.lotterysystem.dao.dataobject.PrizeDO;
import com.example.lotterysystem.dao.mapper.PrizeMapper;
import com.example.lotterysystem.service.dto.FindPageListDTO;
import com.example.lotterysystem.service.dto.PrizeDTO;
import com.example.lotterysystem.service.PictureService;
import com.example.lotterysystem.service.PrizeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class PrizeServiceImpl implements PrizeService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    @Autowired
    private PictureService pictureService;
    @Autowired
    private PrizeMapper prizeMapper;


    @Override
    public Long createPrize(CreatePrizeParam param, MultipartFile picFile) {
        // 上传图片
        String fileName = pictureService.savePicture(picFile);
        // 存库
        PrizeDO prizeDO = new PrizeDO();
        prizeDO.setName(param.getPrizeName());
        prizeDO.setDescription(param.getDescription());
        prizeDO.setImageUrl(fileName);
        prizeDO.setPrice(param.getPrice());
        prizeMapper.insert(prizeDO);
        return prizeDO.getId();



    }

    @Override
    public FindPageListDTO<PrizeDTO> selectPrizeList(PageListParam param) {

        List<PrizeDO> prizeDOS = prizeMapper.selectPrizeList(param.offset(), param.getPageSize());

        List<PrizeDTO> collect = prizeDOS.stream().map(prizeDO -> {
            PrizeDTO prizeDTO = new PrizeDTO();
            prizeDTO.setPrizeId(prizeDO.getId());
            prizeDTO.setPrizeName(prizeDO.getName());
            prizeDTO.setDescription(prizeDO.getDescription());
            prizeDTO.setImageUrl(prizeDO.getImageUrl());
            prizeDTO.setPrice(prizeDO.getPrice());
            return prizeDTO;
        }).collect(Collectors.toList());


//        List<PrizeDTO> prizeDTOList=new ArrayList<>();
//        for (PrizeDO prizeDO : prizeDOS) {
//            PrizeDTO prizeDTO = new PrizeDTO();
//            prizeDTO.setPrizeId(prizeDO.getId());
//            prizeDTO.setPrizeName(prizeDO.getPrizeName());
//            prizeDTO.setDescription(prizeDO.getDescription());
//            prizeDTO.setImageUrl(prizeDO.getImageUrl());
//            prizeDTO.setPrice(prizeDO.getPrice());
//            prizeDTOList.add(prizeDTO);
//        }

        FindPageListDTO<PrizeDTO> findPageListDTO=new FindPageListDTO(param.getPageSize(),collect);

        return findPageListDTO;
    }


}
